package com.es.text;

import com.es.dao.ProductDao;
import com.es.pojo.Product;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author:左泽林
 * @date:日期:2021-08-30-时间:11:42
 * @message:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESSearchTest {

    @Autowired
    private ProductDao productDao;

    /*
     * @author: 左先生
     * @date: 2021-08-30 11:42
     * @description:term查询
     * search(termQueryBuilder) 调用搜索方法，参数查询构造器对象
     */
    @Test
    public void termQuery(){
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("category", "手机");
        Iterable<Product> search = productDao.search(termsQueryBuilder);

        for (Product product : search) {
            System.out.println(product);
        }
    }

    /**
     * term 查询加分页
     */
    @Test
    public void termQueryByPage(){
        int currentPage= 0 ;
        int pageSize = 5;
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");

        Iterable<Product> products = productDao.search(termQueryBuilder,pageRequest);

        for (Product product : products) {
            System.out.println(product);
        }
    }


}
